using System.Collections;
using System.Collections.Generic;

namespace FireStarterModeller.UI.DetailsView
{
    public class ListWithEvents<T> : IList<T>
    {
        public delegate void ItemEvent(T item);

        public ItemEvent ItemAdded;
        public ItemEvent ItemRemoved;

        private readonly IList<T> _bobList = new List<T>();

        public IEnumerator<T> GetEnumerator()
        {
            return _bobList.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(T item)
        {
            _bobList.Add(item);
            FireItemAdded(item);
        }

        private void FireItemAdded(T item)
        {
            if (ItemAdded != null) ItemAdded(item);
        }

        private void FireItemRemoved(T item)
        {
            if (ItemRemoved != null) ItemRemoved(item);
        }

        public void Clear()
        {
            _bobList.Clear();
        }

        public bool Contains(T item)
        {
            return _bobList.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _bobList.CopyTo(array, arrayIndex);
        }

        public bool Remove(T item)
        {
            if (!_bobList.Remove(item)) return false;
            FireItemRemoved(item);
            return true;
        }

        public int Count
        {
            get { return _bobList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _bobList.IsReadOnly; }
        }

        public int IndexOf(T item)
        {
            return _bobList.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _bobList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _bobList.RemoveAt(index);
        }

        public T this[int index]
        {
            get { return _bobList[index]; }
            set { _bobList[index] = value; }
        }
    }
}